//
//  MovieViewController.m
//  豆瓣
//
//  Created by Roach on 16/6/23.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"
#import "MovieDetailsViewController.h"

@interface MovieViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArray;


@end

@implementation MovieViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self config];
    [self values];
    [self creatSubView];
}
- (void)config{
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_collection"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.navigationItem.title = @"电影";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:103 / 255.0 green:155 / 255.0 blue:172 / 255.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.modelArray = [[NSMutableArray alloc]init];
    
}

- (void)rightAction:(UIBarItem *)right {
    
    NSLog(@"右边");
    
}


- (void)values {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *resultArray = [dictionary objectForKey:@"result"];
    for (NSDictionary *dic in resultArray) {
        MovieModel *model = [[MovieModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArray addObject:model];
        [model release];
    }
    
}

- (void)creatSubView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.rowHeight = 150;
    self.tableView.separatorColor = [UIColor blackColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    // 得到当前对应的 model 数据
    
    MovieModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    // 给 cell 的 Model 属性赋值
    
    cell.cellModel = model;
    
    
    return cell;
   
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieDetailsViewController *modc = [[MovieDetailsViewController alloc]init];
    
    modc.movieModel = [self.modelArray objectAtIndex:indexPath.row];
    
    [self.navigationController pushViewController:modc animated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
